package Persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import DAO.DAOAutomovil;
import Modelo.Automovil;
import Modelo.Cliente;
import Modelo.Reserva;;

public class AdministracionPersistenciaReserva {
	
	private static AdministracionPersistenciaReserva instance;
	
	private AdministracionPersistenciaReserva(){
		
	}
	 
	public static AdministracionPersistenciaReserva getInstance(){
		if(instance==null)
			instance = new AdministracionPersistenciaReserva();
		return instance;
	}

	public void insert(Reserva reserva){
		Connection con = PoolConnection.getPoolConnection().getConnection();
		try{
			String query = "SELECT max(nroReserva) FROM Reservas "; 
			PreparedStatement ps = con.prepareStatement(query);
			ResultSet rs = ps.executeQuery();
			//Obtengo el ultimo numero de reserva y le sumo uno.
			Integer nroReserva = null;
			while (rs.next()) {
				nroReserva = rs.getInt(1);				
			}
			if(nroReserva == null)
				nroReserva = 1;
			else
				nroReserva++;
			ps = con.prepareStatement("insert into Reservas values (?,?,?,?,?,?)");
			ps.setInt(1, nroReserva);
			ps.setInt(2, reserva.getCliente().getDni());
			ps.setString (3, reserva.getDesde());
			ps.setString(4, reserva.getAutomovil().getPatente());
			ps.setString(5, reserva.getDesde());
			ps.setString (6,reserva.getHasta());
			ps.execute();
			PoolConnection.getPoolConnection().closeConnections();
		}catch(Exception e){
			System.out.println("Error al insertar Reserva "+e.getMessage());
		}
	}

	public void update(Reserva reserva){
		Connection con = PoolConnection.getPoolConnection().getConnection();
		try{
			PreparedStatement ps = con.prepareStatement("" +
					"update Reservas set patente = ?, dniCliente = ?, nombreCliente = ?, apellidoCliente =? ,desde = ?, hasta = ? , marca = ?, modelo =?" +
					"where nroReserva = ?");
			ps.setString(1, reserva.getAutomovil().getPatente());
			ps.setInt(2, reserva.getCliente().getDni());
			ps.setString(3, reserva.getCliente().getNombre());
			ps.setString (4, reserva.getCliente().getApellido());
			ps.setString(5, reserva.getDesde());
			ps.setString (6,reserva.getHasta());
			ps.setString(7, reserva.getAutomovil().getPatente());
			ps.setString(8, reserva.getAutomovil().getModelo().getModelo());
			ps.setInt(9, reserva.getNumero());
			ps.execute();
			PoolConnection.getPoolConnection().closeConnections();
		}catch(Exception e){
			System.out.println("Error al modificar Reserva"+e.getMessage());
		}
	}
	
	public ArrayList<Reserva> select(){
		ArrayList<Reserva> result = new ArrayList<Reserva>();
		Connection con = PoolConnection.getPoolConnection().getConnection();
	
		try{
			String query = "SELECT * FROM Reservas "; 
			
			PreparedStatement ps = con.prepareStatement(query);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				int dni = rs.getInt(2);
				Cliente cli = AdministracionPersistenciaCliente.getInstance().select(dni);
				String patente = rs.getString(4);
				Automovil automovil = AdministracionPersistenciaAutomovil.getInstance().select(patente);
				Reserva rec = new Reserva(automovil,cli,rs.getString(5),rs.getString(6));	
				result.add(rec);				
			}
			
		
			PoolConnection.getPoolConnection().closeConnections();
		}catch(Exception e){
			System.err.print("Error al Buscar Clientes");
			PoolConnection.getPoolConnection().closeConnections();
		}
		
		return result;
	}

	public void delete(Reserva p){
		Connection con = PoolConnection.getPoolConnection().getConnection();
		try{
						
			PreparedStatement ps2 = con.prepareStatement("delete from Reservas where nroReserva = ?");
			ps2.setInt(1, p.getNumero());
			ps2.execute();
			
			PoolConnection.getPoolConnection().closeConnections();
		}catch(Exception e){
			System.out.println("Error al borrar Reserva "+e.getMessage());
		}
	}

}
